﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class OptionFrm : BaseFrm
    {
        public YY.Common.EventCommon EvntCommon;

        /// <summary>
        /// 选择器窗体返回项
        /// </summary>
        public object SelectorItem { get; set; }

        /// <summary>
        /// 编码规则
        /// </summary>
        public CodeRuleFrm.CodeRule codeRule { get; set; }

        /// <summary>
        /// Treeview加载的通用选项类型
        /// </summary>
        public YY.Common.EnumCommon.OptionType OType { get; set; }

        /// <summary>
        /// 窗体是否处于选择模式
        /// </summary>
        public bool SearchMode { get; set; }

        public OptionFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
            this.codeRule = new CodeRuleFrm.CodeRule();//取默认编码规则
        }

        private void OptionFrm_Load(object sender, EventArgs e)
        {
            YY.Common.EventCommon.OptionUdTVEvnt += new YY.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
            this.treeView1.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseDoubleClick);
            bindTreeView();
            if (!this.SearchMode)
                this.btnConfirm.Visible = false;
        }

        void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (this.SearchMode)
                returnSelectItem();
        }

        #region 处理委托的回调函数
        /// <summary>
        /// 接收处理UpdateTreeViewDlgt
        /// </summary>
        /// <param name="o"></param>
        /// <param name="add"></param>
        private void updateTreeView(object o, EventArgs arg)
        {
            TreeNode node = new TreeNode();
            YY.UI.Common.OperateType opType = Common.OperateType.Add;
            CommonEventArg<TB_Options> argObj = (CommonEventArg<TB_Options>)arg;
            node.Name = argObj.t.id.ToString();
            node.Text = argObj.t.code + "[" + argObj.t.name + "]";
            node.Tag = argObj.t;
            opType = argObj.OpType;


            if (this.treeView1.SelectedNode != null)
            {
                if (opType == Common.OperateType.Add)
                    this.treeView1.SelectedNode.Nodes.Add(node);
                else if (opType == Common.OperateType.Update)
                {
                    this.treeView1.SelectedNode.Text = node.Text;
                    this.treeView1.SelectedNode.Tag = node.Tag;
                }
            }
        }
        #endregion

        private void bindTreeView()
        {
            TB_Options[] options = this.BaseDataProxy.GetOptionTree(this.OType.ToString());
            Common.BindOptionTree(this.treeView1, options, this.OType.ToString());
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            EditOptionFrm frm = new EditOptionFrm();
            frm.EditMode = false;
            int parentId = 0;
            if (this.treeView1.SelectedNode!=null && !string.IsNullOrEmpty(this.treeView1.SelectedNode.Name))
                 parentId = Convert.ToInt32(this.treeView1.SelectedNode.Name);
            frm.ParentId = parentId;
            frm.OType = this.OType;
            frm.codeRule = this.codeRule;
            frm.ShowDialog(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.treeView1.SelectedNode.Name))
            {

                EditOptionFrm frm = new EditOptionFrm();
                frm.EditMode = true;
                frm.Id = Convert.ToInt32(this.treeView1.SelectedNode.Name);
                frm.ShowDialog(this);

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.treeView1.SelectedNode.Name))
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    bool ok = false;
                    ok = this.BaseDataProxy.DeleteOption(Convert.ToInt32(this.treeView1.SelectedNode.Name));
                    if (ok)
                        this.treeView1.Nodes.Remove(this.treeView1.SelectedNode);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string title = this.OType.ToString() + "编码规则";
            CodeRuleFrm frm = new CodeRuleFrm(title);
            frm.Rule = this.codeRule;
            if (this.treeView1.SelectedNode != null && this.treeView1.SelectedNode.Tag != null)
                frm.inHeader = this.treeView1.SelectedNode.Tag.ToString();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.codeRule = frm.Rule;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            returnSelectItem();
        }

        private void returnSelectItem()
        {
            if (this.treeView1.SelectedNode != null && this.treeView1.SelectedNode.Tag!=null)
            {                
                this.SelectorItem = this.treeView1.SelectedNode.Tag;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            YY.Common.EventCommon.OptionUdTVEvnt -= new YY.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
            //YY.Common.EventCommon.WorkstageUdTVEvnt -= new YY.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
        }
    }
}
